package algotithm.leetcode.array.interview1001;

/**
 * @author zhouyanxiang
 * @Date 2021-04-2021/4/4-23:37
 * @Title 面试题 10.01. 合并排序的数组
 */
public class Solution {

    public static void main(String[] args) {
        int[] A = {1,2,3,0,0,0};
        int m = 3;
        int[] B = {2,5,6};
        int n = 3;
        Solution solution = new Solution();
        solution.merge(A,m,B,n);
        for (int i = 0; i < A.length; i++) {
            System.out.print(A[i] + " ");
        }
    }

    public void merge(int[] A, int m, int[] B, int n) {
        int i = m - 1, j = n - 1;
        while(i >= 0 && j >= 0) {
            if (A[i] <= B[j]) {
                A[i+j+1] = B[j--];
            } else {
                A[i+j+1] = A[i--];
            }
        }
        while (j >= 0) {
            A[j] = B[j--];
        }
    }
}
